package com.fhj.bookshop.controller;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhj.bookshop.annotation.LoginToken;
import com.fhj.bookshop.annotation.PassToken;
import com.fhj.bookshop.common.R;
import com.fhj.bookshop.entity.Customer;
import com.fhj.bookshop.entity.Product;
import com.fhj.bookshop.entity.User;
import com.fhj.bookshop.mapper.CustomerMapper;
import com.fhj.bookshop.service.ICustomerService;
import com.fhj.bookshop.service.impl.TokenService;
import javafx.beans.property.IntegerProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fu
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/user")
public class CustomerController {


    @Autowired
    private ICustomerService customerService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CustomerMapper customerMapper;

    @PassToken
    @PostMapping("/register")
    public R userRegister(@RequestBody Customer customer){
        String account=customer.getCustAccount();
        Customer checkAccount=customerService.getByAccount(account);
        if(checkAccount != null){
            return R.error(20001,"账号已被注册");
        }
        customer.setStatus("user");
        customer.setCustTel("未设定");
        customer.setCustAddress("未设定");
        String name= "用户"+RandomUtil.randomString(10);
        customer.setCustName(name);
        customerService.addUser(customer);

        return R.ok("注册成功").setData(customer);
    }

    @PassToken
    @PostMapping("/login")
    public R userLogin(@RequestBody Customer customer){
        String custAccount = customer.getCustAccount();
        String custPwd =customer.getCustPwd();
        Customer customerDB = customerService.getByAccountAndPassword(custAccount,custPwd);
        if(customerDB == null){
            return R.error(20001,"没有该用户");
        }else{
            User user=new User(customerDB.getCustId(),customerDB.getCustAccount(),customerDB.getCustPwd(),"",customerDB.getCustImg());
            String token = tokenService.getToken(user);
            user.setToken(token);
            return R.ok("登录成功").setData(user);
        }
    }

    @LoginToken
    @GetMapping("/getUser/{custId}")
    public R getUser(@PathVariable Integer custId){
        Customer customer=customerService.getById(custId);
        return R.ok("查找用户成功").setData(customer);
    }

    @LoginToken
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody Customer changeForm){
        customerMapper.updateById(changeForm);
        Customer customer=customerMapper.selectById(changeForm.getCustId());
        return R.ok("更新成功").setData(customer);
    }

    @GetMapping("/adminUser/{custId}/{pageCode}/{pageSize}")
    public R otherUserPage(@PathVariable Integer custId, @PathVariable Integer pageCode, @PathVariable Integer pageSize){
        Page<Customer> pageInfo =customerService.otherUserPage(custId,pageCode,pageSize);
        return R.ok("查找用户成功").setData(pageInfo);
    }

    @GetMapping("/changePower/{custId}/{power}")
    public R powerChange(@PathVariable Integer custId,@PathVariable String power){
        customerService.changePower(custId,power);
        return R.ok(custId.toString()+":"+power);
    }

    @GetMapping("/check/{custId},{custAccount}")
    public R checkUser(@PathVariable Integer custId,@PathVariable String custAccount){
        Customer customer = customerMapper.selectById(custId);
        if(Objects.equals(customer.getCustAccount(), custAccount)){
            return R.ok("ok").setData(customer.getStatus());
        }else{
            return R.error("ok").setData("error");
        }
    }

    @GetMapping("/getAvatarUrl/{custId}")
    public R getAvatar(@PathVariable Integer custId){
        Customer customer = customerMapper.selectById(custId);
        String url=customer.getCustImg();
        return R.ok("头像获取成功").setData(url);
    }

    @GetMapping("/getCustName/{custId}")
    public R getCustAccount(@PathVariable Integer custId){
        Customer customer = customerMapper.selectById(custId);
        String account=customer.getCustName();
        return R.ok("头像获取成功").setData(account);
    }

//    简单小测试
//    @GetMapping("getUser/{id}")
//    public Customer getUser(@PathVariable Integer id){
//        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("cust_id",id);
//
//        return customerService.getOne(queryWrapper);
//    }

}
